def flag_max_match(item, b_list, b_max_match):
    # Find match item from end of b_list to begin 
    # default max_match = 0
    cur_max_match = 0
    for i in range(len(b_list)-1, -1, -1):
        if item == b_list[i] and cur_max_match+1>b_max_match[i]['max']:
            # update b_max_match list
            b_max_match[i] = {'max': cur_max_match+1, 'item': 'A'+str(item)}
        else:
            # update current max_match visited
            cur_max_match = b_max_match[i]['max'] if b_max_match[i]['max']>cur_max_match else cur_max_match

def flag_max_matchs(a_list, b_list):
    b_max_match = [{'item':'', 'max':0} for i in range(len(b_list))]
    [flag_max_match(item, b_list, b_max_match) for item in a_list[::-1]]
    return (max([item['max'] for item in b_max_match]), b_max_match)



# Test
a_list = [2,3,1]
b_list = [1,2,3]

print(flag_max_matchs(a_list, b_list))